import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Controls 1.0

Window {
    id: win
    visible: true
    width: _WIDTH
    height: _HEIGHT
    title: qsTr("单人麻将")
    minimumWidth: width
    minimumHeight: height
    maximumHeight: height
    maximumWidth: width

    property real _WIDTH: 1000
    property real _HEIGHT: 562
    property real _FPS: 100
    property real _CARDTYPES: 5
    property real _HOLDCARDS: 5
    property real _SCORESTEP: 100

    property real _INIT: -1
    property real _FA_CARD: 0
    property real _MO_CARD: 1
    property real _XUAN_CARD: 2
    property real _CHU_CARD: 3
    property real _HUPAI: 4
    property real _LIUJU: 5

    property var cardsArray: new Array
    property var cardsUserArray: new Array
    property var selectCardForChu: undefined
    property string tips: ""
    property real cardsLens: cardsArray.length

    property bool gameover: false
    property real score: 0
    property real gameState: _INIT


    Timer{
        id: timer
        interval: _FPS
        repeat: true
        onTriggered: {
            if(!gameover)
            {
                win.update()
            }
        }
    }

    function update()
    {
        // check car
        if(cardsUserArray.length === _CARDTYPES)
        {
            var temp = new Array
            for(var c = 0; c < _CARDTYPES; c++)
            {
                 temp.push(0)
            }

            for(var i in cardsUserArray)
            {
                var card = cardsUserArray[i]
                temp[card.cardType-1] += 1
            }

            // 玩家 是否胡牌
            var card3 = 0
            var card2 = 0
            var cardN = 0
            for(var d = 0; d < _CARDTYPES; d++)
            {
                 if(temp[d] === 3)
                 {
                     card3 += 1
                 }
                 else if(temp[d] === 2)
                 {
                     card2 += 1
                 }
                 else if(temp[d] !== 0){
                     cardN += 1
                 }
            }

            if(cardN === 0 && card2 === 1)
            {
                // 胡牌
                gameState = _HUPAI
                score += _SCORESTEP
            }
        }

        if(gameState === _LIUJU || gameState === _HUPAI)
        {
            gameover = true
        }

        if(!gameover)
        {
            if(cardsArray.length === 0)
            {
                // 无牌可摸
                tips = "流局"
                gameState = _LIUJU
                score -= _SCORESTEP
                if(score <= 0) score = 0
            }
        }
    }

    function getRandom(value)
    {
        Math.seed = (new Date).toLocaleString()
        var r = Math.random()
        var index = Math.floor(value * r)
        return index
    }

    function createCard(type)
    {
        var obj = Qt.createComponent("Card.qml").createObject(itemCards)
        obj.cardId = cardsArray.length
        obj.cardType = type
        obj.cardSrc = "qrc:/images/card" + String(obj.cardType) + ".png"
        obj.x = obj.width * cardsUserArray.length
        cardsUserArray.push(obj)

        // 为玩家手里的牌整理
        sortUserCard()
    }

    function sortUserCard()
    {
        var len = cardsUserArray.length
        for(var m = 0; m < len; ++m)
        {
            for(var n = m + 1; n < len; ++n)
            {
                if(cardsUserArray[m].cardType > cardsUserArray[n].cardType)
                {
                    var temp = cardsUserArray[m]
                    cardsUserArray[m] = cardsUserArray[n]
                    cardsUserArray[n] = temp
                }
            }
        }

        for(var i in cardsUserArray)
        {
            var card = cardsUserArray[i]
            card.x = card.width * i
        }
    }

    function init()
    {
        gameover = false
        gameState = _INIT
        tips = ""
        selectCardForChu = undefined
        // init 牌库 5*4 = 20
        cardsArray = new Array
        for(var i = 1; i <= _CARDTYPES; i++)
        {
            cardsArray.push(i)
            cardsArray.push(i)
            cardsArray.push(i)
            cardsArray.push(i)
        }
        cardsLens = cardsArray.length

        // clear user cards
        for(var i in cardsUserArray)
        {
            var card = cardsUserArray[i]
            card.destroy()
        }
        cardsUserArray = new Array

        // 发牌
        gameState = _FA_CARD
    }

    function faCard()
    {
        // 发牌
        for(var i = 0; i < _CARDTYPES; i++)
        {
            var index = getRandom(cardsArray.length)
            var type = cardsArray[index]

            createCard(type)
            cardsArray.splice(index, 1)
        }
        cardsLens = cardsArray.length
        gameState = _CHU_CARD
    }

    function moCard()
    {
        // 摸牌
        if(cardsArray.length === 0)
        {
            // 无牌可摸
        }
        else{
            var index = getRandom(cardsArray.length)
            var type = cardsArray[index]

            createCard(type)
            gameState = _CHU_CARD
            cardsArray.splice(index, 1)
            cardsLens = cardsArray.length
        }
    }

    function selectCard(valueId, select)
    {
        // 选牌 -> 出牌
        if(select)
        {
            // 取消其他牌的选择
            for(var i in cardsUserArray)
            {
                var card = cardsUserArray[i]
                if(valueId === card.cardId)
                {
                    card.isSelected = true
                    selectCardForChu = card
                }
                else{
                    card.isSelected = false
                }
            }
        }
        else{
            selectCardForChu = undefined
        }
    }

    function chuCard()
    {
        // 出牌
        if(selectCardForChu === undefined || !selectCardForChu)
        {
            tips = "请选择一张牌!"
        }
        else{
            tips = ""
            for(var i in cardsUserArray)
            {
                var card = cardsUserArray[i]
                if(card.cardId === selectCardForChu.cardId)
                {
                    cardsUserArray.splice(i, 1)
                    card.visible = false
                    card.destroy()
                    sortUserCard()
                    gameState = _MO_CARD
                    break
                }
            }
        }
    }

    Component.onCompleted: {
        init()
        timer.restart()
    }

    Rectangle{
        id: root
        width: _WIDTH
        height: _HEIGHT
        color: "white"

        Image{
            id: bg
            anchors.fill: parent
            source: "qrc:/images/bg.png"
        }

        Item{
            id: itemCards
            anchors.horizontalCenter: parent.horizontalCenter
            anchors.bottom: parent.bottom
            anchors.bottomMargin: 100
            width: 101 * _HOLDCARDS
            height: 135
        }

        Row{
            id: row
            anchors.horizontalCenter: parent.horizontalCenter
            anchors.verticalCenter: parent.verticalCenter
            anchors.verticalCenterOffset: -50

            MyButton{
                text: "发牌"
                onClicked: faCard()
                enabled: gameState === _FA_CARD
            }
            MyButton{
                text: "摸牌"
                onClicked: moCard()
                enabled: gameState === _MO_CARD
            }
            MyButton{
                text: "出牌"
                onClicked: chuCard()
                enabled: gameState === _CHU_CARD
            }
            MyButton{
                text: "下一局"
                onClicked: init()
                enabled: gameover
            }
        }

        Item{
            id: itemTip
            anchors.fill: parent
            Text {
                id: name
                color: "red"
                font.pixelSize: 30
                text: tips
                anchors.horizontalCenter: parent.horizontalCenter
                y: 30
            }
        }

        Item{
            id: itemScore
            anchors.fill: parent
            Text{
                font.family: "微软雅黑"
                font.pixelSize: 30
                color: "white"
                text: "分数: " + String(score) + "\n牌库: " + String(cardsLens)
                x: 5
                y: 5
            }
        }

        Item {
            id: imgGameover
            anchors.fill: parent
            visible: gameover
            z: 1

            Text{
                text: gameState === _LIUJU ? "流局" : "胡了"
                font.family: "微软雅黑"
                font.pixelSize: 50
                anchors.horizontalCenter: parent.horizontalCenter
                y: 50
                color: gameState === _LIUJU ? "red": "green"
            }
        }
    }

}
